<template>
  <!-- 主体区域 -->
  <section id="app">
    <!-- 输入框 -->
    <TodoHeader @add="add($event)"></TodoHeader>
    <!-- 列表区域 -->
    <TodoMain :list="list" @del="del($event)"></TodoMain>
    <!-- 统计和清空 -->
    <TodoFooter :len="list.length" @clear="clear"></TodoFooter>

  </section>
</template>

<script>
import TodoHeader from './components/TodoHeader.vue';
import TodoMain from './components/TodoMain.vue';
import TodoFooter from './components/TodoFooter.vue';
const list = [
        { id: 1001, task: '吃饭' },
        { id: 1002, task: '睡觉' },
        { id: 1003, task: '消费' },
      ]
export default {
  components: { TodoHeader, TodoMain,TodoFooter},
  data() {
    return {
      list: JSON.parse(localStorage.getItem("list")) || list,
    }
  },
  methods: {
    add(task) {
      this.list.push({ id: new Date(), task: task })
      task = ''
    },
    del(id) {
      this.list = this.list.filter(item => item.id !== id)
    },
    clear() {
      this.list = []
    }
  },
  watch:{
    list:{
      deep:true,
      handler(newVal){
        localStorage.setItem("list", JSON.stringify(newVal));
      }
    }
  }
}
</script>

<style></style>
